package com.chance.cc.crawler.development.scripts.enorth;

import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerRecordContext;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.pipeline.result.CrawlerData;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.core.script.crawler.CrawlerCommonScript;
import com.chance.cc.crawler.core.selector.Selectable;
import com.chance.cc.crawler.core.tags.crawler.CrawlerResultTags;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerDataType.article;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/11/30 13:45
 * @Description 北方网
 **/
public class EnorthCrawlerScript extends CrawlerCommonScript {

    private Logger log = LoggerFactory.getLogger(EnorthCrawlerScript.class);

    public static final String enorthEntranceUrl = "http://enorth.com.cn/";
    public static final String modelEntranceUrl = "http://(news|economy).enorth.com.cn/";
    public static final String modelUrl = "http://\\S+.enorth.com.cn/(tj|gn|gj|sh)/index.shtml";
    public static final String modelArticleUrl = "http://\\S+.enorth.com.cn/\\S+/|http://news.enorth.com.cn/video/bfws/index.shtml";
    public static final String nextPageUrl = "http://\\S+.enorth.com.cn/system/(more|more2)/\\S*.shtml";
    public static final String articleUrl = "(http|https)://\\S+.enorth.com.cn/system/\\d{4}/\\d{2}/\\d{2}/\\d*.shtml";

    public static final List<String> noModels = Arrays.asList("gnzt","shzt","zgjj","gjjj","cjzt","hj","tzjj","whsc","tjjj","bhxq","bxsc","by","spqh","gsjy","qxfz","tjsj","jrcl"
            ,"gszt","zqgg","zhangdaoda","zhengbuchun","scsj","spqh","bxsc","tzcl","whsc","bxgl","cp");//不采集的模块

    /**
     * 脚本domain定义
     *
     * @return
     */
    @Override
    public String domain() {
        return "enorth";
    }

    /**
     * 进入脚本的正则列表
     */
    @Override
    public void initUrlRegulars() {
        addUrlRegular(enorthEntranceUrl);
        addUrlRegular(modelEntranceUrl);
        addUrlRegular(modelUrl);
        addUrlRegular(modelArticleUrl);
        addUrlRegular(nextPageUrl);
        addUrlRegular(articleUrl);
    }

    /**
     * 是否执行脚本 ： 输入数据检查，合格的才进入脚本
     *
     * @param crawlerRequestRecord
     * @return
     */
    @Override
    public boolean crawlerCheck(CrawlerRequestRecord crawlerRequestRecord) {
        return true;
    }


    /**
     * 解析链接方法
     *
     * @param crawlerRequestRecord
     * @param httpPage
     * @return
     */
    @Override
    public List<CrawlerRequestRecord> parseLinks(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {
        List<CrawlerRequestRecord> parsedLinks = new ArrayList<CrawlerRequestRecord>();

        String requestUrl = httpPage.getRequest().getUrl();
        String webSite = requestUrl.substring(requestUrl.indexOf("//") + 2, requestUrl.indexOf("."));

        if (requestUrl.matches(enorthEntranceUrl)) {
            List<String> all = httpPage.getHtml().xpath("//div[@class=\"daohang1 zi12 hanggao24\"]/a/@href").all();
            for (String url : all) {
                if (url.matches(modelEntranceUrl)) {
                    CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                            .turnPageRequest(crawlerRequestRecord)
                            .httpUrl(url)
                            .recordKey(url)
                            .notFilterRecord()
                            .releaseTime(System.currentTimeMillis())
                            .build();

                    parsedLinks.add(itemRecord);
                }
            }
        } else if (requestUrl.matches(modelEntranceUrl)) {
            List<String> all = new LinkedList<>();
            if(webSite.matches("news")){
                all = httpPage.getHtml().xpath("//div[@class=\"daohang5 yanse_bai\"]/a/@href").all();
            }else if(webSite.matches("economy")){
                all = httpPage.getHtml().xpath("//div[contains(@class,'zi14')]/a/@href").all();
            }
            for (String url : all) {
                if((webSite.matches("news") && url.matches(modelUrl)) || (webSite.matches("economy") && url.matches(modelArticleUrl))){
                    String model = "";
                    if(webSite.matches("economy")){
                        String[] split = url.split("/");
                        model = split[split.length - 1];
                    }
                    if(webSite.matches("economy") && noModels.contains(model)){
                        continue;
                    }
                    CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                            .turnPageRequest(crawlerRequestRecord)
                            .httpUrl(url)
                            .recordKey(url)
                            .notFilterRecord()
                            .releaseTime(System.currentTimeMillis())
                            .build();

                    parsedLinks.add(itemRecord);
                }else{
                    continue;
                }
            }
        } else if (requestUrl.matches(modelUrl)) {
            List<String> all = httpPage.getHtml().xpath("//div[@align=\"right\"]//a/@href").all();
            for (String url : all) {
                if (url.matches(modelArticleUrl)) {
                    String[] split = url.split("/");
                    String key = split[split.length - 1];
                    if (!noModels.contains(key)) {
                        CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                                .turnPageRequest(crawlerRequestRecord)
                                .httpUrl(url)
                                .recordKey(url)
                                .notFilterRecord()
                                .releaseTime(System.currentTimeMillis())
                                .build();

                        parsedLinks.add(itemRecord);
                    }
                }
            }
        } else if (requestUrl.matches(modelArticleUrl) || requestUrl.matches(nextPageUrl)) {
            //解析下一页的地址
            String nextUrl = "";
            if (requestUrl.matches("http://news.enorth.com.cn/tj/tjyw/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001019000000000/0011/17001019000000000_00001115.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/wytd/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001009000000000/0038/17001009000000000_00003874.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/chengjianxinwen/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001006000000000/0001/17001006000000000_00000177.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/fwkx/tianqixinxi/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001014001000000/0001/17001014001000000_00000185.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/fwkx/jiaotongxinxi/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001014002000000/0001/17001014002000000_00000139.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/shipin/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001001000000000/0009/17001001000000000_00000987.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/dushixiaofei/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001008000000000/0004/17001008000000000_00000415.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/jiaoyuxinxi/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001027000000000/0001/17001027000000000_00000159.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/jmrs/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001002000000000/0001/17001002000000000_00000103.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/qxdt/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001010000000000/0001/17001010000000000_00000147.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/tj/fazhixinwen/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17001028000000000/0005/17001028000000000_00000573.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/video/bfws/index.shtml")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17034002000000000/0003/17034002000000000_00000331.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/gnyw/")) {
                nextUrl = "http://news.enorth.com.cn/system/more2/17002001000000000/00/01/27/17002001000000000_00012730.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/gdzl/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002002000000000/0042/17002002000000000_00004270.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/gat/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002010000000000/0009/17002010000000000_00000972.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/ssqd/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002003000000000/0013/17002003000000000_00001342.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/wjwd/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002016000000000/0001/17002016000000000_00000110.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/rsbd/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002015000000000/0001/17002015000000000_00000138.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/shipin/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002007000000000/0008/17002007000000000_00000877.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gn/jcwz/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17002009000000000/0000/17002009000000000_00000023.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/gjyw/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003008000000000/0067/17003008000000000_00006723.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/qqzh/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003002000000000/0016/17003002000000000_00001609.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/jsjj/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003005000000000/0001/17003005000000000_00000144.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/sspl/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003006000000000/0001/17003006000000000_00000193.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/jdrw/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003003000000000/0000/17003003000000000_00000061.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/gj/shipin/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17003004000000000/0008/17003004000000000_00000832.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/sh/shgj/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17004002000000000/0026/17004002000000000_00002688.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/sh/qwqs/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17004004000000000/0001/17004004000000000_00000117.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/sh/shrw/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17004005000000000/0001/17004005000000000_00000102.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/sh/alcz/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17004003000000000/0004/17004003000000000_00000429.shtml";
            }else if(requestUrl.matches("http://news.enorth.com.cn/sh/shipin/")) {
                nextUrl = "http://news.enorth.com.cn/system/more/17004001000000000/0014/17004001000000000_00001451.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/zgjj/cjxx/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14001003000000000/0013/14001003000000000_00001361.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/zqsc/hqgs/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14005001000000000/0012/14005001000000000_00001206.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jjsh/shxf/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14022001000000000/0004/14022001000000000_00000473.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/yjjy/cjrw/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14015001000000000/0001/14015001000000000_00000106.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/zgjj/cjsd/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14001004000000000/0003/14001004000000000_00000356.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/jrdt/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14023004000000000/0003/14023004000000000_00000382.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/zqqt/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14023003000000000/0002/14023003000000000_00000202.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/gd/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14009000000000000/0061/14009000000000000_00006197.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/zgjj/hgjj/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14001001000000000/0011/14001001000000000_00001132.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/tjjj/jjzx/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14003001000000000/0004/14003001000000000_00000447.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/tjjj/xfms/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14003005000000000/0003/14003005000000000_00000319.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/tjjj/tjgp/jrxx/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14003002003000000/0000/14003002003000000_00000081.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/zqsc/zqfx/jggd/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14005002013000000/0016/14005002013000000_00001609.shtml";
            }else if(requestUrl.matches( "http://economy.enorth.com.cn/zqsc/zqfx/ggfx/")) {
                nextUrl = "http://economy.enorth.com.cn/system/more/14005002023000000/0005/14005002023000000_00000541.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/bxsc/tjbx/")){
                nextUrl = "http://economy.enorth.com.cn/system/more/14023005006000000/0000/14023005006000000_00000020.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/jrdt/")){
                nextUrl = "http://economy.enorth.com.cn/system/more/14023004000000000/0003/14023004000000000_00000382.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/tzjj/fxdt/")){
                nextUrl = "http://economy.enorth.com.cn/system/more/14023002002000000/0003/14023002002000000_00000301.shtml";
            }else if(requestUrl.matches("http://economy.enorth.com.cn/jrgj/zqqt/")){
                nextUrl = "http://economy.enorth.com.cn/system/more/14023003000000000/0002/14023003000000000_00000202.shtml";
            }
            else if(requestUrl.matches(nextPageUrl)){
                    String substring = requestUrl.substring(requestUrl.lastIndexOf("_") + 1, requestUrl.lastIndexOf("."));
                    nextUrl = requestUrl.replaceAll(substring, autoAddZero(substring));
            }

            if (StringUtils.isNotEmpty(nextUrl)) {
                CrawlerRequestRecord modelRecord = CrawlerRequestRecord.builder()
                        .turnPageRequest(crawlerRequestRecord)
                        .recordKey(nextUrl)
                        .httpUrl(nextUrl)
                        .releaseTime(System.currentTimeMillis())
                        .build();

                parsedLinks.add(modelRecord);
            }

            //解析文章列表
            List<String> nodes = new LinkedList<>();
            if(webSite.matches("news")){
                nodes = httpPage.getHtml().xpath("//td[@valign=\"top\"]/table/tbody/tr/td/table/tbody/tr/td[@class=\"zi14\"]/text()/../a/@href").all();
            }else if(webSite.matches("economy")){
                nodes = httpPage.getHtml().xpath("//td[@class=\"hanggao26 zi14 zz1\"]/text()/../a/@href").all();
            }
            for (String itemUrl : nodes) {
                if (StringUtils.isEmpty(itemUrl)) {
                    continue;
                }

                String releaseTime = getString("\\d{4}/\\d{2}/\\d{2}",itemUrl);
                if (StringUtils.isEmpty(releaseTime)) {
                    continue;
                }
                try {
                    Long releaseTimeToLong = DateUtils.parseDate(releaseTime, "yyyy/MM/dd").getTime();

                    CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                            .itemPageRequest(crawlerRequestRecord)
                            .recordKey(itemUrl)
                            .httpUrl(itemUrl)
                            .releaseTime(releaseTimeToLong)
                            .build();

                    parsedLinks.add(itemRecord);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        } else if (requestUrl.matches(articleUrl)) {
            String refresh = httpPage.getHtml().xpath("//meta[@http-equiv=\"refresh\"]").get();
            if (StringUtils.isNotEmpty(refresh)) {
                String url = getString("url=\\S*\"", refresh);
                String itemUrl = StringUtils.isNotEmpty(url) ? url.substring(url.indexOf("=") + 1, url.lastIndexOf("\"")) : "";
                if(itemUrl.equals(requestUrl)){
                    crawlerRequestRecord.tagsCreator().resultTags().addResultDataType(article);
                }else if(itemUrl.matches(articleUrl)){
                    CrawlerRequestRecord itemRecord = CrawlerRequestRecord.builder()
                            .itemPageRequest(crawlerRequestRecord)
                            .recordKey(itemUrl)
                            .httpUrl(itemUrl)
                            .releaseTime(crawlerRequestRecord.getReleaseTime())
                            .build();

                    parsedLinks.add(itemRecord);
                }else{
                    log.info("该网页(“"+ requestUrl +"”)跳转到其他网页(“" + itemUrl+ "”)");
                }
            } else {
                crawlerRequestRecord.tagsCreator().resultTags().addResultDataType(article);
            }
        }

        return parsedLinks;
    }

    @Override
    public List<CrawlerData> washPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        List<CrawlerData> crawlerDataList = new ArrayList<>();
        CrawlerResultTags crawlerResultTags = crawlerRecord.tagsCreator().resultTags();

        String charset = page.getCharset();
        if(!"GBK".equals(charset)){
            if (crawlerResultTags.hasDataType(article)) {
                crawlerDataList.add(washArticle(crawlerRecord, page));
            }
        }else{
            log.info("该文章(“" +page.getRequest().getUrl() +"”)是图片类型的文章");
        }

        return crawlerDataList;
    }

    public CrawlerData washArticle(CrawlerRequestRecord crawlerRequestRecord, HttpPage httpPage) {

        String title = httpPage.getHtml().xpath("//i[@class=\"col-sm-8\"]/text()|//b[@class=\"col-sm-8\"]").get();
        if (StringUtils.isEmpty(title)) {
            title = httpPage.getHtml().xpath("//h2[@class=\"col-sm-12\"]/text()").get();
        }

        List<Selectable> authorAndSourceTime = httpPage.getHtml().xpath("//p[@class=\"col-sm-6 info\"]/span|//p[@class=\"col-sm-8 info\"]/span").nodes();
        String author = "";
        String source = "";
        String releaseTime = "";
        String bianji = "";
        for (Selectable selectable : authorAndSourceTime) {
            String data = selectable.xpath("./text()").get();
            if (data.contains("来源：")) {
                source = StringUtils.isNotEmpty(selectable.xpath("./a/text()").get()) ? selectable.xpath("./a/text()").get() : StringEscapeUtils.unescapeHtml4(data.substring(data.indexOf("：") + 1));
                source = source.trim();
            }
            if (data.contains("作者：")) {
                author = data.substring(data.indexOf("：") + 1);
            }
            if(data.contains("编辑：")){
                bianji = data.substring(data.indexOf("：") + 1);
            }
            if (data.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
                releaseTime = data;
            }
        }

        List<String> articleTextList = httpPage.getHtml().xpath("//div[@id=\"article\"]//text()").all();
        StringBuffer conents = new StringBuffer();
        for (String articleText : articleTextList) {
            if (StringUtils.isNotEmpty(articleText) && !articleText.contains("hwplayerloaded(") && !articleText.contains("showPlayer(")) {
                conents.append(StringEscapeUtils.unescapeHtml4(articleText)).append(" ");
            }
        }

        String itemUrl = httpPage.getRequest().getUrl();
        String articleKey = itemUrl.substring(itemUrl.lastIndexOf("/") + 1, itemUrl.lastIndexOf("."));

        CrawlerData crawlerData = null;
        try {
            crawlerData = CrawlerData.builder()
                    .data(crawlerRequestRecord, httpPage)
                    .dataId(StringUtils.joinWith("-", crawlerRequestRecord.getDomain(), CrawlerEnum.CrawlerDataType.article.enumVal(), articleKey))
                    .resultLabelTag(CrawlerEnum.CrawlerDataType.valueOf("article"))
                    .url(itemUrl)
                    .releaseTime(DateUtils.parseDate(releaseTime, "yyyy-MM-dd HH:mm:ss").getTime())
                    .addContentKV(Field_Content, conents.toString().trim())
                    .addContentKV(Field_Title, StringEscapeUtils.unescapeHtml4(title).trim())
//                    .addContentKV(Field_Content,bianji)
                    .addContentKV(Field_Source, source)
                    .addContentKV(Field_Author, author)
                    .build();
        } catch (ParseException e) {
            log.error("时间格式错误");
        }

        return crawlerData;

    }

    @Override
    public void afterExecute(CrawlerRecordContext crawlerRecordContext) {

    }

    /**
     * 获取指定格式的字符串
     *
     * @param regx
     * @param input
     * @return
     */
    private static String getString(String regx, String input) {
        Pattern compile = Pattern.compile(regx);
        Matcher matcher = compile.matcher(input);
        List<String> urlList = new ArrayList<>();
        while (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    /**
     * @param num
     * @return java.lang.String
     * @author Zhao.Hhuan
     * @date 2020/10/13 17:41
     * @description: 将数字自动减一，同时在数字左边自动添加0来补充位数
     **/
    private String autoAddZero(String num) {
        int length = num.length();
        int count = Integer.parseInt(num);
        NumberFormat instance = NumberFormat.getInstance();
        instance.setGroupingUsed(false);
        instance.setMaximumIntegerDigits(length);
        instance.setMinimumIntegerDigits(length);
        return instance.format(count - 1);
    }

}
